package com.example.customer.service;

import com.example.customer.web.ViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service

public class UserService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;// Eureka客户端，可以获取到服务实例信息


    public ViewVo queryUserByIds() {

        // String baseUrl = "http://192.168.136.115:8081/user/";
        // 根据服务名称，获取服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        // 因为只有一个UserService,因此我们直接get(0)获取
        ServiceInstance instance = instances.get(0);
        // 获取ip和端口信息
//        System.out.println(instance.getHost() + ":" + instance.getPort());
        String baseUrl = "http://" + instance.getHost() + ":" + instance.getPort() + "/user";

        ViewVo users = this.restTemplate.getForObject(baseUrl, ViewVo.class);

//        ids.forEach(id -> {
//            // 我们测试多次查询，
//            users.add(this.restTemplate.getForObject(baseUrl + id, User.class));
//            // 每次间隔500毫秒
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
        return users;
    }
}
